package net.jiupinshi.dao;

import net.jiupinshi.daoProvider.ExpenseApplyProvider;
import net.jiupinshi.daoProvider.StoreProvider;
import net.jiupinshi.dto.ExpenseApplyDTO;
import net.jiupinshi.dto.StoreSimpleDTO;
import net.jiupinshi.dto.UserDTO;
import net.jiupinshi.entity.ExpenseApply;
import net.jiupinshi.entity.Store;
import net.jiupinshi.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @Despription: 需要修改
 * @Author: penghui.guo
 * @Date: 2018/10/6 18:05
 */
public interface ExpenseApplyDao {

    int insertExpenseApplys(@Param("expenseApplies") List<ExpenseApply> expenseApplies);

    @Select("select id, apply_user_name as applyUserName, expense_user_name as expenseUserName, apply_user_id as applyUserId" +
            ", expense_user_id as expenseUserId, apply_store_name as applyStoreName, expense_store_name as expenseStoreName" +
            ", apply_store_id as applyStoreId, expense_store_id as expenseStoreId from expense_apply where id = #{id}")
    ExpenseApply getExpenseApplyById(Long id);

    @Select("select ea.id, ea.code, ea.goods_name as goodsName, ea.create_time as createTime, apply_user_name as applyUserName, expense_user_name as expenseUserName" +
            ", expense_store_name as expenseStoreName,  u.name as operateUserName from  expense_apply ea, user u where ea.id = #{id} and ea.apply_user_id = #{applyUserId} and ea.operate_user_id = u.id")
    ExpenseApplyDTO getExpenseApplyByUid(Long id, Long applyUserId);

    List<ExpenseApplyDTO> getExpenseApplys(@Param("startDate")String startDate,
                                           @Param("endDate")String endDate,
                                           @Param("applyUserId") Long applyUserId,
                                           @Param("storeId") Long storeId,
                                           @Param("status") Byte status,
                                           @Param("expenseUserId")Long expenseUserId,
                                           @Param("start")int start,
                                           @Param("count")int count);

    int getExpenseApplysCount(@Param("startDate")String startDate,
                              @Param("endDate")String endDate,
                                           @Param("applyUserId") Long applyUserId,
                                           @Param("storeId") Long storeId,
                                           @Param("status") Byte status,
                                           @Param("expenseUserId")Long expenseUserId);

    @Update("update expense_apply set operate_user_id = #{operateUserId}, status = #{status}, operate_time = now()" +
            " where status = 1 and id in (${ids})")
    int updateExpenseApplys(@Param("operateUserId") Long operateUserId,
                            @Param("status") byte status,
                            @Param("ids") String ids);

    @Select("select apply_user_name as userName, apply_Store_id as id, apply_Store_name as name from" +
            " expense_apply where expense_user_id = #{expenseUserId} and status = 1 group by apply_store_id")
    List<StoreSimpleDTO> getToConfirmStoresByEUid(Long expenseUserId);

    @Select("select count(id) from expense_apply where expense_user_id = #{expenseUserId} and status = 1")
    Integer getToConfirmCountByEUid(Long expenseUserId);

    @Select("select content, amount, number from expense_apply where apply_user_id = #{applyUserId} and status != 0")
    List<ExpenseApplyDTO> getExpenseApplysByUid(Long applyUserId);

    @Select("select content, amount, number from expense_apply where " +
            "apply_user_id = #{applyUserId} and parent_id = #{parentId} and status != 0")
    List<ExpenseApply> getExpenseApplysByPid(Long applyUserId, Long parentId);

    @Select("select apply_user_id as id, apply_user_name as name from expense_apply " +
            "where expense_user_id = #{expenseUserId} and status = 1 group by apply_user_id, apply_user_name")
    List<UserDTO> getApplyUsersByExpenseUid(Long expenseUserId);

    @SelectProvider(type = ExpenseApplyProvider.class, method = "getExpenseApplyByStatusUid")
    List<ExpenseApplyDTO> getExpenseApplyByStatusUid(@Param("applyUserId") Long applyUserId, @Param("operateUserId")Long operateUserId,
                                                     @Param("status") Byte status, @Param("start") int start, @Param("count") int count);

    @SelectProvider(type = ExpenseApplyProvider.class, method = "getExpenseApplyCountByStatusUid")
    int getExpenseApplyCountByStatusUid(@Param("applyUserId") Long applyUserId,@Param("operateUserId")Long operateUserId, @Param("status") Byte status);

    @Select("select s.id, s.name, s.user_name as userName, s.user_id as userId from expense_apply ea, store s where" +
            " ea.id = #{id} and ea.expense_store_id = s.id")
    Store getStoreByApplyExpenseId(Long id);

    List<Map<String, Object>> getExpenseApplyList(@Param("startDate")String startDate,
                                                  @Param("endDate")String endDate,
                                  @Param("storeId") Long storeId,
                                  @Param("status") Byte status);
}
